package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import model.Stack;
import service.LocationNotFoundException;
import service.Service;
import dao.Dao;

/**
 * @author Alexander Shidlovsky
 */
public class MainFrame extends JFrame {
	private JPanel contentPane;
	private Controller controller = new Controller();
	private JButton btnAddStack, btnManageData, btnPickNextStack;
	private NotStockPanel pnlNotStock;
	private StockPanel pnlStock;
	private FinishedPanel pnlFinished;
	private WastesPanel pnlWastes;
	private AllStacksPanel pnlAllStacks;
	private JTabbedPane tpnlMain;
	private JMenuItem menuItem1 = new JMenuItem("About");
	private JMenuItem menuItem2 = new JMenuItem("Open manager");
	private JMenuItem menuItem3 = new JMenuItem("Create product type");
	private JMenuItem menuItem4 = new JMenuItem("Create process");
	private JMenuItem menuItem5 = new JMenuItem("Add stack");
	private JMenuItem menuItem9 = new JMenuItem("Create objects");

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Carletti - Production management system");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(250, 100, 830, 620);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(5, 0));

		JPanel pnlButtons = new JPanel();

		contentPane.add(pnlButtons, BorderLayout.WEST);
		pnlButtons.setLayout(new GridLayout(15, 0, 10, 5));

		tpnlMain = new JTabbedPane(JTabbedPane.TOP);

		contentPane.add(tpnlMain, BorderLayout.CENTER);

		Dao dao = Dao.getInstance();

		pnlStock = new StockPanel(dao.getStackWidth(), dao.getStackHeight());

		pnlNotStock = new NotStockPanel();
		pnlFinished = new FinishedPanel();
		pnlWastes = new WastesPanel();
		pnlAllStacks = new AllStacksPanel();

		tpnlMain.addTab("In Stock", pnlStock);
		tpnlMain.addTab("Not In Stock", pnlNotStock);
		tpnlMain.addTab("Finished", pnlFinished);
		tpnlMain.addTab("Wastes", pnlWastes);
		tpnlMain.addTab("All", pnlAllStacks);

		btnAddStack = new JButton("Add stack");
		btnPickNextStack = new JButton("Pick next stack");
		btnManageData = new JButton("Manage data");

		pnlButtons.add(btnAddStack);
		pnlButtons.add(btnPickNextStack);
		pnlButtons.add(btnManageData);

		btnAddStack.addActionListener(controller);
		btnManageData.addActionListener(controller);
		btnPickNextStack.addActionListener(controller);

		JMenuBar menuBar;
		JMenu menuHelp, menuManage, menuStack;

		menuBar = new JMenuBar();

		menuHelp = new JMenu("Help");
		menuManage = new JMenu("Manage data");
		menuStack = new JMenu("Stack operations");

		menuItem1.addActionListener(controller);
		menuItem2.addActionListener(controller);
		menuItem3.addActionListener(controller);
		menuItem4.addActionListener(controller);
		menuItem5.addActionListener(controller);
		menuItem9.addActionListener(controller);

		menuStack.add(menuItem5);

		menuManage.add(menuItem2);
		menuManage.add(menuItem3);
		menuManage.add(menuItem4);
		menuManage.add(menuItem9);
		menuHelp.add(menuItem1);

		menuBar.add(menuStack);
		menuBar.add(menuManage);
		menuBar.add(menuHelp);

		setJMenuBar(menuBar);

	}

	public StockPanel getStockPanel() {
		return pnlStock;
	}

	public void updateView() {
		for (int i = 0; i < tpnlMain.getTabCount(); i++) {
			Component c = tpnlMain.getComponentAt(i);
			((Updatable) c).updateView();
		}
	}

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnAddStack) {
				AddStack fr = new AddStack(MainFrame.this);
				fr.setVisible(true);
			} else if (e.getSource() == btnManageData) {
				ManageData fr = new ManageData();
				fr.setVisible(true);

			} else if (e.getSource() == btnPickNextStack) {
				Stack s = Service.getInstance().pickNextStack();
				if (s != null) {
					StackInfoDialog info = new StackInfoDialog(s);
					info.setVisible(true);
				} else {
					NoStackFoundDialog dlgNoStackFound = new NoStackFoundDialog();
					dlgNoStackFound.setVisible(true);
				}
			} else if (e.getSource() == menuItem1) {
				// About
				AboutDialog ad = new AboutDialog();
				ad.setVisible(true);

			} else if (e.getSource() == menuItem2) {
				// Open manager
				ManageData fr = new ManageData();
				fr.setVisible(true);
			} else if (e.getSource() == menuItem3) {
				// Create product type
				AddEditType aet = new AddEditType(MainFrame.this);
				aet.setVisible(true);
			} else if (e.getSource() == menuItem4) {
				// Create process
				AddEditProcess aep = new AddEditProcess(MainFrame.this);
				aep.setVisible(true);
			} else if (e.getSource() == menuItem5) {
				// Add stack
				AddStack fr = new AddStack(MainFrame.this);
				fr.setVisible(true);
			} else if (e.getSource() == menuItem9) {
				// Create some objects
				try {
					Service.getInstance().createSomeObjects();
				} catch (LocationNotFoundException e1) {
					JOptionPane.showMessageDialog(MainFrame.this,
							"Could not find location", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}

		}
	}

}
